﻿#region
using System;
using System.IO;
using System.Windows.Forms;
using AfricaDownloader.Properties;

#endregion

namespace AfricaDownloader
{
    public partial class CommandControl : UserControl
    {
        public CommandControl()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            if (DesignMode)
                return;

            if (string.IsNullOrEmpty(Settings.Default.DownloadRoot))
                Settings.Default.DownloadRoot = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

            txtBrowse.Text = Settings.Default.DownloadRoot;

            txtUrl.Text = "http://live.afreeca.com:8079/app/index.cgi?szType=read_ucc_bbs&szBjId=originalsj&nStationNo=9502455&nTitleNo=24586476&szSkin=";

            txtUrl.Focus();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            if (fbdBrowser.ShowDialog() != DialogResult.OK)
                return;

            txtBrowse.Text = fbdBrowser.SelectedPath;
            Settings.Default.DownloadRoot = txtBrowse.Text;
        }

        private void btnStart_Click(object sender, EventArgs e)
        {
            if (txtBrowse.Text.Length == 0 || txtUrl.Text.Length == 0)
                return;

            if (Directory.Exists(txtBrowse.Text) == false)
                return;

            Enabled = false;

            OnStarted(txtUrl.Text);
        }

        #region Started event things for C# 3.0
        public event EventHandler<StartedEventArgs> Started;

        protected virtual void OnStarted(StartedEventArgs e)
        {
            if (Started != null)
                Started(this, e);
        }

        private StartedEventArgs OnStarted(string africaUrl)
        {
            StartedEventArgs args = new StartedEventArgs(africaUrl);
            OnStarted(args);

            return args;
        }

        public class StartedEventArgs : EventArgs
        {
            public string AfricaUrl { get; set; }

            public StartedEventArgs()
            {
            }

            public StartedEventArgs(string africaUrl)
            {
                AfricaUrl = africaUrl;
            }
        }
        #endregion
    }
}